---
title: "main"
output: pdf_document
date: "2025-07-17"
---

# packages
```{r}
library(cobalt)
library(WeightIt)
library(stargazer)
library(interplot)
library(dplyr)
```

# load data
```{r}
load("data.rda")
```

# entropy balancing
```{r}
original <- bal.tab(final_monitor ~ domesticobs + first_multiparty +
                     executive + durable_lagged + Ln_GDP_lagged +
                     sanction + Ln_foreign_aid_lagged,
        data = dat_vdem2, estimand = "ATT", thresholds = c(m = .05))


wei <- weightit(final_monitor ~ domesticobs + executive + first_multiparty + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged, data = dat_vdem2, estimand = "ATT", method = "ebal")
```

# Table 1
```{r}
mod_wei0 <- lm(sub_free_and_fair ~ final_monitor + domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_wei1 <- lm(voterregistry ~ final_monitor + domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_wei2 <- lm(votebuy ~ final_monitor + domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_wei3 <- lm(govintimidat ~ final_monitor + domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_wei4 <- lm(nonstateviolence ~ final_monitor + domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

stargazer(mod_wei0, mod_wei3, mod_wei4, mod_wei1, mod_wei2,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          column.sep.width="1pt", font.size="small", digits=2, 
          dep.var.caption="Outcome variable")
```

# Table 2
```{r}
mod_wei <- lm(sub_free_and_fair ~ final_monitor*domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_weic <- lm(voterregistry ~ final_monitor*domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_weid <- lm(votebuy ~ final_monitor*domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_weif <- lm(govintimidat ~ final_monitor*domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

mod_weig <- lm(nonstateviolence ~ final_monitor*domesticobs + first_multiparty + executive + factor(country) + durable_lagged + Ln_GDP_lagged + sanction + Ln_foreign_aid_lagged 
            , dat_vdem2, weight = wei$weights)

stargazer(mod_wei, mod_weif,mod_weig, mod_weic, mod_weid,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          column.sep.width="1pt", font.size="small", digits=2, 
          dep.var.caption="Outcome variable")
```

# Figure 1
```{r}
df_eq <- interplot(m = mod_wei, var1 = "final_monitor", var2 = "domesticobs", ci = 0.95, plot = FALSE)

df_eq <- df_eq %>%
    mutate(
        domestic_label = factor(domesticobs, levels = c(0, 1), labels = c("Absence", "Presence")),
        sig_color = case_when(
            lb > 0 ~ ifelse(domesticobs == 0, "red", "blue"),
            ub < 0 ~ ifelse(domesticobs == 0, "red", "blue"),
            TRUE ~ "black"
        ),
        sig_shape = ifelse(domesticobs == 0, 17, 16)
    )

ggplot(df_eq, aes(x = coef, y = "Election Quality", group = domestic_label)) +
    geom_errorbarh(aes(xmin = lb, xmax = ub, color = sig_color),
                   position = position_dodge(width = 0.5), height = 0.15) +
    geom_point(aes(shape = domestic_label, color = sig_color),
               position = position_dodge(width = 0.5), size = 2.5) +
    geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
    scale_shape_manual(
        name = "Domestic Observer",
        values = c("Presence" = 16, "Absence" = 17),
        breaks = c("Presence", "Absence")
    ) +
    scale_color_manual(values = c("black" = "black", "red" = "red", "blue" = "blue"), guide = "none") +
    scale_x_continuous(
        limits = c(-0.4, 0.3),
        expand = expansion(mult = c(0, 0))
    ) +
    labs(x = NULL, y = NULL) +
    ggtitle("Marginal Effect of International Observer") +
    theme_classic() +
    theme(
        plot.title = element_text(hjust = 0.5, size = 13, margin = margin(b = 10)),
        axis.title.x = element_blank(),
        axis.text.x = element_text(size = 11),
        axis.text.y = element_text(size = 12),
        legend.position = "bottom",
        legend.title = element_text(size = 12),
        legend.text = element_text(size = 11)
    )
```

# Figure 2
```{r}
df_c <- interplot(m = mod_weic, var1 = "final_monitor", var2 = "domesticobs", ci = .95, plot = FALSE) %>%
  mutate(fraud_type = "Voter Registry")
df_d <- interplot(m = mod_weid, var1 = "final_monitor", var2 = "domesticobs", ci = .95, plot = FALSE) %>%
  mutate(fraud_type = "Vote Buying")
df_f <- interplot(m = mod_weif, var1 = "final_monitor", var2 = "domesticobs", ci = .95, plot = FALSE) %>%
  mutate(fraud_type = "Government Intimidation")
df_g <- interplot(m = mod_weig, var1 = "final_monitor", var2 = "domesticobs", ci = .95, plot = FALSE) %>%
  mutate(fraud_type = "Non-state Electoral Violence")

df_combined <- bind_rows(df_c, df_d, df_f, df_g) %>%
  mutate(
    domestic_label = factor(domesticobs, levels = c(0, 1), labels = c("Absence", "Presence")),
    sig_color = case_when(
      lb > 0 ~ ifelse(domesticobs == 0, "red", "blue"),
      ub < 0 ~ ifelse(domesticobs == 0, "red", "blue"),
      TRUE ~ "black"
    ),
    sig_shape = ifelse(domesticobs == 0, 17, 16),  
    fraud_type = factor(fraud_type,
                        levels = c("Vote Buying", 
                                   "Voter Registry", 
                                   "Non-state Electoral Violence", 
                                   "Government Intimidation"))
  )

ggplot(df_combined, aes(x = coef, y = fraud_type, group = domestic_label)) +
  geom_errorbarh(aes(xmin = lb, xmax = ub, color = sig_color), 
                 position = position_dodge(width = 0.5), height = 0.15) +
  geom_point(aes(shape = domestic_label, color = sig_color), 
             position = position_dodge(width = 0.5), size = 2.5) +
  geom_vline(xintercept = 0, linetype = "dashed", color = "red") +
  scale_shape_manual(name = "Domestic Observer", 
                     values = c("Presence" = 16, "Absence" = 17),
    breaks = c("Presence", "Absence")) +
  scale_color_manual(values = c("black" = "black", "red" = "red", "blue" = "blue"), 
                     guide = "none") +
  scale_x_continuous(
    limits = c(-0.3, 0.55),
    expand = expansion(mult = c(0, 0))
  ) +
  labs(x = NULL, y = NULL) +
  ggtitle("Marginal Effect of International Observer") +
  theme_classic() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 13, margin = margin(b = 10)),
    axis.title.x = element_blank(),
    axis.text.y = element_text(size = 12),
    axis.title.x.bottom = element_blank(),
    legend.position = "bottom",
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 11)
  )
```
